#include "epoll.h"

//创建epoll实例、监听文件描述符
struct epoll_event * epoll_init(int * epollfd){
    struct epoll_event * p;
    *epollfd = epoll_create1(0);
    if(-1 == *epollfd)
        error_log("epoll_init::epoll_create1.");
    //内存申请，注意回收
    p = (struct epoll_event *)calloc(EPOLL_EVENT_SIZE,sizeof(struct epoll_event));
    if(NULL == p)
        error_log("epoll_init::error_log.");
    return p;
}
void epoll_destroy(struct epoll_event * epevent){
    debug_log("epoll_destroy");
    free(epevent);
}
//将文件描述符加入epoll监听队列
void epoll_add_fd(int addfd){
    debug_log("epoll_add_fd");
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = addfd;
    int ret = epoll_ctl(epfd,EPOLL_CTL_ADD,addfd,&event);
    if(-1 == ret)
        error_log("epoll_add_fd::epoll_ctl");
}
//将文件描述符移出epoll监听队列
void epoll_del_fd(int delfd){
    debug_log("epoll_del_fd");
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = delfd;
    int ret = epoll_ctl(epfd,EPOLL_CTL_DEL,delfd,&event);
    if(-1 == ret)
        error_log("epoll_del_fd::epoll_ctl");
}